//
//  JPTitleView.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/5/18.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit
import Kingfisher

class TitleChujiaImgView: UIView {
    var listType = TBandJPType.TB
    var one = ZyaZtbContentModel(JSON:["":""])!
    private lazy var imgbg:UIImageView = {
        var imgbg = UIImageView()
        imgbg.contentMode = .scaleAspectFill
        //        imgBtn.imageView?.contentMode = .scaleAspectFit
        imgbg.clipsToBounds = true
        return imgbg
    }()
    
    lazy var chujiaImg:UIImageView = {
        var v  = UIImageView()
        v.image = UIImage(named: "chujia_chuizi")
        return v
    }()
    lazy var chujiaLab:UILabel = {
        var lab :UILabel = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.text = "-"
        lab.numberOfLines = 2
        return lab
    }()
    
    // 标题字体大小自适应 http://59.59.58.36:3316/zentao/task-view-22661.html
    lazy var titleLab: AutoAdjustLabel = {
        let lab = AutoAdjustLabel()
        lab.font = UIFont.boldSystemFont(ofSize: 17)
        lab.setMaxLines(2)
        lab.setFontStyle(bold: true)
        return lab
    }()
    
    lazy var subTitleLab:UILabel = {
        var v :UILabel = UILabel()
        v.font = UIFont.systemFont(ofSize: 14)
        v.textColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        return v
    }()
    lazy var mychujiaLab:UIButton = {
        var v  = UIButton()
        v.setTitle("我的出价", for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        v.setTitleColor(UIColor(kshex: tenderBlue), for: .normal)
        return v
    }()
    lazy var lineV:UIView = {
       var v = UIView()
        v.backgroundColor = UIColor(kshex: tenderBlue)
        return v
    }()
   var withImg = false
 
    convenience init(withImg:Bool) {
      
        self.init(frame:CGRect.zero)
        self.withImg = withImg
        self.backgroundColor = .white
        configWidget()
    }
    
  
    private override init(frame: CGRect) {
        super.init(frame: frame)
        
        
    }
    func configWidget() {
        if withImg {
            addSubview(imgbg)
            imgbg.snp.makeConstraints { (make) in
                make.left.top.right.equalTo(self)
                make.height.equalTo(self.snp.width).multipliedBy(0.474)
            }
        }
        
        addSubview(titleLab)
        addSubview(subTitleLab)
       addSubview(mychujiaLab)
        addSubview(lineV)
        addSubview(chujiaLab)
        addSubview(chujiaImg)
      
  
        titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(12)
            make.right.equalTo(subTitleLab.snp.left).offset(-1)
            if withImg {
                make.top.equalTo(imgbg.snp.bottom).offset(5)
            } else {
                make.top.equalTo(self).offset(5)
            }
        }
    
        subTitleLab.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(titleLab)
            make.width.equalTo(62)
        }
        chujiaImg.snp.makeConstraints { (make) in
            make.top.equalTo(titleLab.snp.bottom).offset(6)
            make.size.equalTo(CGSize(width: 18, height: 18))
            make.left.equalTo(titleLab)
            make.bottom.equalToSuperview().offset(-10)
        }
        chujiaLab.snp.makeConstraints { (make) in
            make.centerY.equalTo(chujiaImg)
            make.left.equalTo(chujiaImg.snp.right).offset(1)
            make.right.equalTo(mychujiaLab.snp.left).offset(-3)

        }
        mychujiaLab.snp.makeConstraints { make in
            make.right.equalTo(subTitleLab)
            make.centerY.equalTo(chujiaImg)
            make.width.equalTo(58)
            make.height.equalTo(17)
        }
        lineV.snp.makeConstraints { make in
            make.width.equalTo(mychujiaLab)
            make.height.equalTo(1)
            make.top.equalTo(mychujiaLab.snp.bottom)
            make.centerX.equalTo(mychujiaLab)
        }
        mychujiaLab.addTouchUpInSideAction { _ in
            if let api = self.recordApi {
          
 
                    KSNetwork.shared.doRequest(api) { (data:BaseResult<TBRecordResult>) in
                        if data.flag == TRUE {
                            if let infos = data.infoArrCombine {
                                self.showListView(one:self.one,infos: infos)
                            }
                        } else {
                            KSToast(data.errorCombine.unwrap)
                        }
                    }
        
            } else {
                KSToast("联系管理员")
            }
           
        }
    }
    
  
    var recordApi:API?
    
    func showListView(one:ZyaZtbContentModel,infos:[TBRecordResult]) {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 8
        view.frame = CGRect(x:20,y:180,width:KSGlobal.screenW - 40,height:KSGlobal.screenH - 360)

        let newsView = ZyaTbRecordList()
        newsView.one = one
        newsView.dataArray = infos
        newsView.reloadData()
         view.addSubview(newsView)
        newsView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5))
        }
//                newsView.layer.masksToBounds = true
//                newsView.layer.cornerRadius = 12
         
        
        let topViewController = RouterUtils.currentTopViewController()!
        topViewController.lew_presentPopupView( view, animation: LewPopupViewAnimationSpring()) {
            print("动画结束")
        }
    }
    
    func showBannerImg(one:ZyaZtbContentModel) {
        self.one = one
        if self.withImg {
            
            var imgstr = one.zMainUrl.unwrap
            if !imgstr.contains("sid") {
                imgstr = "\(imgstr)?sid=\(one.sid.unwrap)"
            }
            
            if  let imageUrl = URL(string:imgstr) {
                let resource = KF.ImageResource(downloadURL: imageUrl)
                self.imgbg.kf.setImage(with: resource, placeholder: UIImage(named:"defaultImg_content"))
             }
        }
       
    }
    func setChujiaShow(one:ZyaZtbContentModel? = nil,title:String,count:String,price:String,unit:String,rank:String,z_end_price_rank:String,z_parallel:String) {
        if one.isSome {
            self.one = one!
        }
        
        self.titleLab.text = title
        self.subTitleLab.text = "\(intTran(count))次出价"
        
        if z_end_price_rank != "" {
            self.chujiaImg.isHidden = false
            self.chujiaLab.text = z_end_price_rank
            self.chujiaLab.textColor = .red
        } else {
            if price != "" && rank != "" {
                let fullLine = NSMutableAttributedString()
                // 把冒号换成空格，有点距离，又符合需求
                let blackattributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
                let redattributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

                fullLine.append(NSAttributedString(string: "您的出价", attributes: blackattributes))
                
                fullLine.append(NSAttributedString(string: price, attributes: redattributes))
                fullLine.append(NSAttributedString(string: "元/\(unit),排在第", attributes: blackattributes))
                fullLine.append(NSAttributedString(string: "\(rank)", attributes: redattributes))
                fullLine.append(NSAttributedString(string: "位\(z_parallel)", attributes: blackattributes))

                self.chujiaLab.attributedText = fullLine
                self.chujiaImg.isHidden = false
            } else {
                self.chujiaLab.text = ""
                self.chujiaImg.isHidden = true
            }
        }
        
  
       
        
       
        
        
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
    
}
 

